package dao

import (
	"album/model"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"listenboos-common/bloom"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"listenboos-common/tools"
	"strconv"
	"time"
)

type AlbumInfoDao struct {
	conn *gorms.GormConn
}

func NewAlbumInfoDao(db *db.ListenDB) *AlbumInfoDao {
	return &AlbumInfoDao{
		conn: gorms.New(db.Conn),
	}
}
func (u *AlbumInfoDao) GetAllCategoryList(ctx context.Context) (albums []*model.AlbumInfo, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.AlbumInfo{}).Find(&albums).Error
	if err != nil {
		return nil, nil
	}
	return albums, nil
}
func (u *AlbumInfoDao) Save(ctx context.Context, conn db.DBConn, data *model.AlbumInfo) (int64, error) {
	u.conn = conn.(*gorms.GormConn)
	tx := u.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	data.CreateTime = now
	data.UpdateTime = now
	err := tx.Save(&data).Error
	return data.Id, err
}
func (u *AlbumInfoDao) DeleteAlbumInfo(ctx context.Context, conn db.DBConn, albumId int64, userId int64) error {
	data := model.AlbumInfo{}
	u.conn = conn.(*gorms.GormConn)
	tx := u.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	data.UpdateTime = now
	err := tx.Model(&model.AlbumInfo{}).Where("id = ? and user_id = ?", albumId, userId).Update("is_deleted", 1).Error
	return err
}
func (u *AlbumInfoDao) FindAlbumByIdAndUserId(ctx context.Context, albumId int64, userId int64) (album *model.AlbumInfo, err error) {
	session := u.conn.Session(ctx)
	fmt.Println(userId)
	err = session.Model(&model.AlbumInfo{}).Where("id = ? and is_deleted !=1", albumId).Limit(1).Take(&album).Error
	if err != nil {
		return nil, err
	}
	return album, nil
}
func (u *AlbumInfoDao) FindAlbumById(ctx context.Context, id int64) (album *model.AlbumInfo, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.AlbumInfo{}).Where("id  = ? and is_deleted !=1", id).Limit(1).Take(&album).Error
	if err != nil {
		return nil, err
	}
	return album, nil
}
func (u *AlbumInfoDao) FindAlbumListByUserId(ctx context.Context, userId int64) (albums []*model.AlbumInfo, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.AlbumInfo{}).Where(" user_id = ? and is_deleted !=1", userId).Find(&albums).Error
	if err != nil {
		return nil, err
	}
	return albums, nil
}
func (u *AlbumInfoDao) FindByUserId(ctx context.Context, id int64, num string, size string) (albums []*model.AlbumInfo, err error) {
	session := u.conn.Session(ctx)
	pageNum, _ := strconv.Atoi(num)
	pageSize, _ := strconv.Atoi(size)
	offset := (pageNum - 1) * pageSize
	err = session.Model(&model.AlbumInfo{}).Where("user_id = ? and is_deleted !=1", id).Limit(pageSize).Offset(offset).Find(&albums).Error
	if err != nil {
		return nil, err
	}
	return albums, nil
}
func (u *AlbumInfoDao) FindByAlbumId() ([]int64, error) {
	session := u.conn.Session(context.Background())
	var ids []int64
	err := session.Model(&model.AlbumStat{}).Select("id").Scan(&ids).Error
	if err != nil {
		return nil, err
	}
	return ids, nil
}
func (u *AlbumInfoDao) InitBloom() {
	// 把专辑id插入到bloom里
	ids, err := u.FindByAlbumId()
	if err != nil {
		logx.Error(err)
		return
	}
	for _, id := range ids {
		bloom.Bloom.Filter.Add([]byte(fmt.Sprintf("%d", id)))
	}
	return
}
